package com.vedio.service;

import com.vedio.dao.DataDao;
import com.vedio.entity.Data;
import com.vedio.mapper.DataMapper;
import com.vedio.mapper.MovieMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DataService implements DataDao {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2.获取SqlSessionFactory对象，用它执行sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //3.执行sql
    //List<Product> products = sqlSession.selectList("test.selectAll");
    //获取mapper接口的代理对象
    DataMapper dataMapper = sqlSession.getMapper(DataMapper.class);

    public DataService() throws IOException {
    }

    @Override
    public List<Data> selectAll() throws IOException {
        List<Data> data = dataMapper.selectAll();
        sqlSession.close();
        return data;
    }

    @Override
    public Data selectById(Integer id) {
        Data data = dataMapper.selectById(id);
        sqlSession.close();
        return data;
    }

    @Override
    public int add(Data data) {
        int add = dataMapper.add(data);
        sqlSession.close();
        return add;
    }

    @Override
    public int update(Data data) {
        int update = dataMapper.update(data);
        sqlSession.close();
        return update;
    }

    @Override
    public int deleteById(Integer id) {
        int result = dataMapper.deleteById(id);
        sqlSession.close();
        return result;
    }
}
